<script setup name="ArticleChannel">
import { Edit, Delete } from '@element-plus/icons-vue'
import { articleGetChannelService, articleDeleteChannelService } from '@/api/article'
import { ref, onMounted } from 'vue'
import ChannelEdit from './components/ChannelEdit.vue'

let channelList = ref([])
let loading = ref(false)
let dialog = ref()
/**
 * 获取数据
 */
const getChannelList = async () => {
  loading.value = true
  const res = await articleGetChannelService()
  channelList.value = res.data
  loading.value = false
}
/**
 * 刷新数据
 */
const onSuccess = async () => {
  await getChannelList()
}
/**
 * 编辑
 * @param row 当前行的数据
 */
const onEditChannel = (row) => {
  dialog.value.open(row)
}
/**
 * 添加
 */
const onAddChannel = () => {
  dialog.value.open({})
}
/**
 * 删除
 * @param row 当前行的数据
 */
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('您确定要删除该分类么', '温馨提示', {
    type: 'warning',
    confrimButtonText: '确定',
    cancelButtonText: '取消',
  })
  await articleDeleteChannelService(row.id)
  ElMessage.success('删除成功~')
  await getChannelList()
}
// 生命周期函数
onMounted(async () => {
  await getChannelList()
})
</script>

<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannel"> 添加分类 </el-button>
    </template>
    <el-table :data="channelList" style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="cate_name" label="分类名称" />
      <el-table-column prop="cate_alias" label="分类别名" />
      <el-table-column label="操作" width="150px" align="center">
        <template #default="{ row }" style="margin-right: 10px">
          <el-button
            type="primary"
            circle
            plain
            :icon="Edit"
            @click="onEditChannel(row)"
          ></el-button>
          <el-button
            type="danger"
            circle
            plain
            :icon="Delete"
            @click="onDelChannel(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="还没有数据呢~" />
      </template>
    </el-table>

    <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
  </page-container>
</template>

<style scoped lang="scss"></style>
